/**
 * MenuFrameNewGame.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2011 Roslan Amir. All rights reserved.
 */

package com.roslan.games.moo3d.ui.menu;

import com.ardor3d.extension.ui.UIButton;
import com.ardor3d.extension.ui.UILabel;
import com.ardor3d.extension.ui.UIPanel;
import com.ardor3d.extension.ui.UIRadioButton;
import com.ardor3d.extension.ui.event.ActionEvent;
import com.ardor3d.extension.ui.event.ActionListener;
import com.ardor3d.extension.ui.layout.AnchorLayout;
import com.ardor3d.extension.ui.layout.AnchorLayoutData;
import com.ardor3d.extension.ui.layout.RowLayout;
import com.ardor3d.extension.ui.util.Alignment;
import com.ardor3d.extension.ui.util.ButtonGroup;
import com.ardor3d.renderer.Camera;
import com.roslan.games.moo3d.GameData.Actions;

/**
 * Comments go here.
 * 
 * @author Roslan Amir
 * @version 1.0 - Jan 27, 2011
 */
public class MenuFrameNewGame extends MenuFrameBase {

	/**
	 * Default no-arg constructor.
	 * 
	 * @param camera
	 */
	public MenuFrameNewGame(Camera camera) {
		super("New Game");

		/*
		 * Galaxy Size selection options
		 */

		final UIPanel hpanel1 = new UIPanel();
		hpanel1.setLayout(new AnchorLayout());
		hpanel1.setMargin(FRAME_MARGINS);
		hpanel1.setBorder(SOLID_BORDER);
		vpanel.add(hpanel1);

		final UILabel galaxySizeLabel = new UILabel("[b]Galaxy Size:[/b]");
		galaxySizeLabel.setStyledText(true);
		galaxySizeLabel.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel1, Alignment.TOP_LEFT, 4, -17));
		hpanel1.add(galaxySizeLabel);

		final ButtonGroup galaxySizeGroup = new ButtonGroup();

		final UIRadioButton galaxySizeSmall = new UIRadioButton("[b]Small[/b]");
		galaxySizeSmall.setStyledText(true);
		galaxySizeSmall.setSelectable(true);
		galaxySizeSmall.setSelected(gameData.galaxySizeIndex == 0);
		galaxySizeSmall.setGroup(galaxySizeGroup);
		galaxySizeSmall.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel1, Alignment.TOP_LEFT, 104, -17));
		galaxySizeSmall.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.galaxySizeIndex = 0;
			}
		});
		hpanel1.add(galaxySizeSmall);

		final UIRadioButton galaxySizeMedium = new UIRadioButton("[b]Medium[/b]");
		galaxySizeMedium.setStyledText(true);
		galaxySizeMedium.setSelectable(true);
		galaxySizeMedium.setSelected(gameData.galaxySizeIndex == 1);
		galaxySizeMedium.setGroup(galaxySizeGroup);
		galaxySizeMedium.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel1, Alignment.TOP_LEFT, 174, -17));
		galaxySizeMedium.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.galaxySizeIndex = 1;
			}
		});
		hpanel1.add(galaxySizeMedium);

		final UIRadioButton galaxySizeLarge = new UIRadioButton("[b]Large[/b]");
		galaxySizeLarge.setStyledText(true);
		galaxySizeLarge.setSelectable(true);
		galaxySizeLarge.setSelected(gameData.galaxySizeIndex == 2);
		galaxySizeLarge.setGroup(galaxySizeGroup);
		galaxySizeLarge.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel1, Alignment.TOP_LEFT, 264, -17));
		galaxySizeLarge.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.galaxySizeIndex = 2;
			}
		});
		hpanel1.add(galaxySizeLarge);

		final UIRadioButton galaxySizeHuge = new UIRadioButton("[b]Huge[/b]");
		galaxySizeHuge.setStyledText(true);
		galaxySizeHuge.setSelectable(true);
		galaxySizeHuge.setSelected(gameData.galaxySizeIndex == 3);
		galaxySizeHuge.setGroup(galaxySizeGroup);
		galaxySizeHuge.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel1, Alignment.TOP_LEFT, 354, -17));
		galaxySizeHuge.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.galaxySizeIndex = 3;
			}
		});
		hpanel1.add(galaxySizeHuge);

		/*
		 * Difficulty selection options
		 */

		final UIPanel hpanel2 = new UIPanel();
		hpanel2.setLayout(new AnchorLayout());
		hpanel2.setMargin(FRAME_MARGINS);
		hpanel2.setBorder(SOLID_BORDER);
		vpanel.add(hpanel2);

		final UILabel difficultyLabel = new UILabel("[b]Difficulty:[/b]");
		difficultyLabel.setStyledText(true);
		difficultyLabel.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel2, Alignment.TOP_LEFT, 4, -17));
		hpanel2.add(difficultyLabel);

		final ButtonGroup difficultyGroup = new ButtonGroup();

		final UIRadioButton difficultyEasy = new UIRadioButton("[b]Easy[/b]");
		difficultyEasy.setStyledText(true);
		difficultyEasy.setSelectable(true);
		difficultyEasy.setSelected(true);
		difficultyEasy.setGroup(difficultyGroup);
		difficultyEasy.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel2, Alignment.TOP_LEFT, 104, -17));
		difficultyEasy.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.difficultyIndex = 0;
			}
		});
		hpanel2.add(difficultyEasy);

		final UIRadioButton difficultySimple = new UIRadioButton("[b]Simple[/b]");
		difficultySimple.setStyledText(true);
		difficultySimple.setSelectable(true);
		difficultySimple.setSelected(false);
		difficultySimple.setGroup(difficultyGroup);
		difficultySimple.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel2, Alignment.TOP_LEFT, 174, -17));
		difficultySimple.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.difficultyIndex = 1;
			}
		});
		hpanel2.add(difficultySimple);

		final UIRadioButton difficultyAverage = new UIRadioButton("[b]Average[/b]");
		difficultyAverage.setStyledText(true);
		difficultyAverage.setSelectable(true);
		difficultyAverage.setSelected(false);
		difficultyAverage.setGroup(difficultyGroup);
		difficultyAverage.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel2, Alignment.TOP_LEFT, 264, -17));
		difficultyAverage.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.difficultyIndex = 2;
			}
		});
		hpanel2.add(difficultyAverage);

		final UIRadioButton difficultyHard = new UIRadioButton("[b]Hard[/b]");
		difficultyHard.setStyledText(true);
		difficultyHard.setSelectable(true);
		difficultyHard.setSelected(false);
		difficultyHard.setGroup(difficultyGroup);
		difficultyHard.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel2, Alignment.TOP_LEFT, 354, -17));
		difficultyHard.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.difficultyIndex = 3;
			}
		});
		hpanel2.add(difficultyHard);

		final UIRadioButton difficultyImpossible = new UIRadioButton("[b]Impossible[/b]");
		difficultyImpossible.setStyledText(true);
		difficultyImpossible.setSelectable(true);
		difficultyImpossible.setSelected(false);
		difficultyImpossible.setGroup(difficultyGroup);
		difficultyImpossible.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel2, Alignment.TOP_LEFT, 434, -17));
		difficultyImpossible.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.difficultyIndex = 4;
			}
		});
		hpanel2.add(difficultyImpossible);

		/*
		 * Number of Opponents selection options
		 */

		final UIPanel hpanel3 = new UIPanel();
		hpanel3.setLayout(new AnchorLayout());
		hpanel3.setMargin(FRAME_MARGINS);
		hpanel3.setBorder(SOLID_BORDER);
		vpanel.add(hpanel3);

		final UILabel opponentsLabel = new UILabel("[b]Opponents:[/b]");
		opponentsLabel.setStyledText(true);
		opponentsLabel.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel3, Alignment.TOP_LEFT, 4, -17));
		hpanel3.add(opponentsLabel);

		final ButtonGroup opponentsGroup = new ButtonGroup();

		final UIRadioButton opponentsOne = new UIRadioButton("[b]One[/b]");
		opponentsOne.setStyledText(true);
		opponentsOne.setSelectable(true);
		opponentsOne.setSelected(true);
		opponentsOne.setGroup(opponentsGroup);
		opponentsOne.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel3, Alignment.TOP_LEFT, 104, -17));
		opponentsOne.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.raceCount = 2; // including player race
			}
		});
		hpanel3.add(opponentsOne);

		final UIRadioButton opponentsTwo = new UIRadioButton("[b]Two[/b]");
		opponentsTwo.setStyledText(true);
		opponentsTwo.setSelectable(true);
		opponentsTwo.setSelected(false);
		opponentsTwo.setGroup(opponentsGroup);
		opponentsTwo.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel3, Alignment.TOP_LEFT, 174, -17));
		opponentsTwo.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.raceCount = 3;
			}
		});
		hpanel3.add(opponentsTwo);

		final UIRadioButton opponentsThree = new UIRadioButton("[b]Three[/b]");
		opponentsThree.setStyledText(true);
		opponentsThree.setSelectable(true);
		opponentsThree.setSelected(false);
		opponentsThree.setGroup(opponentsGroup);
		opponentsThree.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel3, Alignment.TOP_LEFT, 264, -17));
		opponentsThree.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.raceCount = 4;
			}
		});
		hpanel3.add(opponentsThree);

		final UIRadioButton opponentsFour = new UIRadioButton("[b]Four[/b]");
		opponentsFour.setStyledText(true);
		opponentsFour.setSelectable(true);
		opponentsFour.setSelected(false);
		opponentsFour.setGroup(opponentsGroup);
		opponentsFour.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel3, Alignment.TOP_LEFT, 354, -17));
		opponentsFour.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.raceCount = 5;
			}
		});
		hpanel3.add(opponentsFour);

		final UIRadioButton opponentsFive = new UIRadioButton("[b]Five[/b]");
		opponentsFive.setStyledText(true);
		opponentsFive.setSelectable(true);
		opponentsFive.setSelected(false);
		opponentsFive.setGroup(opponentsGroup);
		opponentsFive.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, hpanel3, Alignment.TOP_LEFT, 434, -17));
		opponentsFive.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.raceCount = 6;
			}
		});
		hpanel3.add(opponentsFive);

		/*
		 * Panel with a horizontal layout
		 */
		final UIPanel hpanel4 = new UIPanel();
		hpanel4.setLayout(new RowLayout(true));
		hpanel4.setMargin(FRAME_MARGINS);
		hpanel4.setBorder(SOLID_BORDER);
		vpanel.add(hpanel4);

		/*
		 * Back button
		 */
		final UIButton backButton = new UIButton("[b]Back[/b]");
		backButton.setStyledText(true);
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_MAIN_MENU;
			}
		});
		hpanel4.add(backButton);

		/*
		 * Continue button
		 */
		final UIButton continueButton = new UIButton("[b]Continue[/b]");
		continueButton.setStyledText(true);
		continueButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_CHOOSE_RACE_MENU;
			}
		});
		hpanel4.add(continueButton);

		/*
		 * Finalization
		 */
		this.setName("New Game");
		this.setContentPanel(vpanel);
		super.setUpFrame();
	}

	/**
	 * Update the contents of this frame before showing it.
	 */
	public void update() {
		UIPanel vpanel = this.getContentPanel();

		UIPanel hpanel1 = (UIPanel) vpanel.getChild(0);
		UIRadioButton galaxySizeSmall = (UIRadioButton) hpanel1.getChild(1);
		galaxySizeSmall.setSelected(gameData.galaxySizeIndex == 0);
		UIRadioButton galaxySizeMedium = (UIRadioButton) hpanel1.getChild(2);
		galaxySizeMedium.setSelected(gameData.galaxySizeIndex == 1);
		UIRadioButton galaxySizeLarge = (UIRadioButton) hpanel1.getChild(3);
		galaxySizeLarge.setSelected(gameData.galaxySizeIndex == 2);
		UIRadioButton galaxySizeHuge = (UIRadioButton) hpanel1.getChild(4);
		galaxySizeHuge.setSelected(gameData.galaxySizeIndex == 3);

		UIPanel hpanel2 = (UIPanel) vpanel.getChild(1);
		UIRadioButton difficultyEasy = (UIRadioButton) hpanel2.getChild(1);
		difficultyEasy.setSelected(gameData.difficultyIndex == 0);
		UIRadioButton difficultySimple = (UIRadioButton) hpanel2.getChild(2);
		difficultySimple.setSelected(gameData.difficultyIndex == 1);
		UIRadioButton difficultyAverage = (UIRadioButton) hpanel2.getChild(3);
		difficultyAverage.setSelected(gameData.difficultyIndex == 2);
		UIRadioButton difficultyHard = (UIRadioButton) hpanel2.getChild(4);
		difficultyHard.setSelected(gameData.difficultyIndex == 3);
		UIRadioButton difficultyImpossible = (UIRadioButton) hpanel2.getChild(5);
		difficultyImpossible.setSelected(gameData.difficultyIndex == 4);

		UIPanel hpanel3 = (UIPanel) vpanel.getChild(2);
		UIRadioButton opponentsOne = (UIRadioButton) hpanel3.getChild(1);
		opponentsOne.setSelected(gameData.raceCount == 2);
		UIRadioButton opponentsTwo = (UIRadioButton) hpanel3.getChild(2);
		opponentsTwo.setSelected(gameData.raceCount == 3);
		UIRadioButton opponentsThree = (UIRadioButton) hpanel3.getChild(3);
		opponentsThree.setSelected(gameData.raceCount == 4);
		UIRadioButton opponentsFour = (UIRadioButton) hpanel3.getChild(4);
		opponentsFour.setSelected(gameData.raceCount == 5);
		UIRadioButton opponentsFive = (UIRadioButton) hpanel3.getChild(5);
		opponentsFive.setSelected(gameData.raceCount == 6);
	}

}
